<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 头部 -->
      <el-row class="headBox">
        <div class="headLeft">
          <span class="keyword">关键字</span>
          <el-input
            placeholder="根据编号搜索"
            v-model="keyword"
            size="small"
          ></el-input>
        </div>
        <div class="headRight">
          <el-button size="small" @click="clear">清除</el-button>
          <el-button type="primary" size="small" @click="search">搜索</el-button>
        </div>
      </el-row>
      <!-- 头部-提示 -->
      <div style="margin-bottom: 15px">
        <el-alert
          :title="'数据一共' + total + '条'"
          type="info"
          show-icon
          :closable="false"
        ></el-alert>
      </div>
      <!-- 主体-表格 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="id" label="编号" width="220"></el-table-column>
        <el-table-column label="题型" width="70">
          <template slot-scope="scope">
           {{trans(scope.row.questionType)}}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="230">
         <template slot-scope="scope">
              <div v-for="(item,index) in scope.row.questionIDs" :key="index">
                <span class="serialNum" @click="DialogShow(item)">{{item.number}}</span>
              </div>
         </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="200"></el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)"></el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)"></el-table-column>
        <el-table-column prop="userName" label="录入人"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" icon="el-icon-delete" circle plain @click="del(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="margin-top: 15px; text-align: right">
        <!-- background 数字加个背景 -->
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[20, 30, 50, 100]"
          :page-size="pagesize"
          layout="prev, pager, next,sizes,jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 弹框 -->
    <el-dialog
    title="题目预览"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="dialogVisible">
      <!-- <page :show-dialog.sync="dialogVisible"></page> -->
      <!-- <page @close="dialogVisible=false" :subject="subject" v-if="dialogVisible"></page> -->

      <RandomsForm @close="dialogVisible=false" :subject="subject" v-if="dialogVisible"></RandomsForm>
    </el-dialog>
		<drag></drag>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import RandomsForm from './components/questions-randoms-previews.vue'
// import Page from '@/components/questions/page.vue'
// import RandomsForm from '../components/questions-preview.vue'

// 枚举文件
import { questionType } from '@/api/hmmm/constants.js'
export default {
  data () {
    return {
      subject: '', // 学科
      total: 100, // 总条数
      keyword: '', // 关键字
      page: 1, // 页码
      pagesize: 20, // 页容量
      list: [], // 数据列表
      dialogVisible: false // 弹框显示和隐藏
    }
  },
  components: { RandomsForm },
  created () {
    this.getList()
  },
  methods: {
    // 枚举形式
    trans (type) {
      // 转换枚举文件格式
      // **************************【把数组合并为一个对象 reduce方法】************************** //
      // let arr = [{ a: 1, b: 10 }, { a: 2, b: 20 }, { a: 3, b: 30 }]
      // let newArr = arr.reduce((pre, item) => {
      //     return {...pre,[item.a]: item.b }}
      // }, {})
      const meiju = questionType.reduce((pre, item) => { return { ...pre, [item.value]: item.label } }, {})
      return meiju[type]
    },
    // 页容量变化
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1 // 第一页展示
      this.getList()
    },
    // 页码变化
    handleCurrentChange (val) {
      this.page = val
      this.getList()
    },
    /**
     * 获取数据列表
     */
    async getList () {
      const { data } = await randoms({ page: this.page, pagesize: this.pagesize, keyword: this.keyword })
      // console.log(data)
      this.total = data.counts
      this.list = data.items
    },
    /**
     * 点击清空按钮
     */
    clear () {
      this.keyword = ''
      this.page = 1 // 第一页展示
      this.getList()
    },
    /**
     * 点击搜索按钮
     */
    search () {
      this.page = 1 // 第一页展示
      this.getList()
    },
    /**
     * 点击删除按钮
     */
    del (row) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', { type: 'warning' }).then(async () => {
        await removeRandoms(row)
        this.$message.success('删除数据成功')
        // 当删最后一条时,改变页码
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        this.getList()
      }).catch(() => {
        this.$message.warning('取消删除')
      })
    },
    // 显示弹框
    DialogShow (subject) {
      this.subject = subject
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped >
.container {
  padding: 10px;
}
.headBox {
  height: 30px;
  margin-bottom: 15px;
}
.headLeft {
  display: flex;
  float: left;
  height: 30px;
  line-height: 30px;
  margin-left: 30px;
}

.keyword {
  margin-top: 3px;
  width: 70px;
  font-size: 14px;
  font-weight: 700;
}
.headRight {
  float: right;
}
.serialNum{
  color: rgb(0, 153, 255);
  cursor: pointer;
}
  .el-button--danger.is-plain:focus {
     color: #F56C6C;
      background: #fef0f0;
      border-color: #fbc4c4;
  }

.el-button--danger.is-plain:hover {
    background: #F56C6C;
    border-color: #F56C6C;
    color: #FFF;
}
</style>
